//
//  RWSetPasswordViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/7.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWSetPasswordViewController.h"
#import "FTYInfoVertifyTools.h"

@interface RWSetPasswordViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *nPasswordTF;

@end

@implementation RWSetPasswordViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation];
    [self setSetPasswordUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)loadDatas{
    
    
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"设置密码";
}

- (void)setSetPasswordUI{
    
    _setPasswordTV = [[FTYTouchTableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_setPasswordTV];
    _setPasswordTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _setPasswordTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _setPasswordTV.scrollEnabled = NO;
    _setPasswordTV.dataSource = self;
    _setPasswordTV.delegate = self;
    _setPasswordTV.touchDelegate = self;
    _setPasswordTV.tableFooterView = [self setFooterUI];
}

#pragma mark - Config Invoke Methods

- (UIView *)setFooterUI{
    
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT)];
    
    FTYButton *modiftyBtn = [FTYButton createButtonWithTitle:@"完成" tColor:[UIColor blackColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [footerV addSubview:modiftyBtn];
    [modiftyBtn addTarget:self action:@selector(modiftyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [modiftyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 15.*FTY_WIDTH_BASE *2);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * const modifyPasswordID = @"modifyPasswordID";
    UITableViewCell *modifyPasswordCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:modifyPasswordID];
    
    modifyPasswordCell.imageView.image = [UIImage imageNamed:@"code_icon"];
    
    UITextField *textField = [[UITextField alloc] init];
    [modifyPasswordCell addSubview:textField];
    textField.placeholder = @"请输入新密码";
    textField.font = FTY_FONTTSIZE(15);
    textField.delegate = self;
    [textField sizeToFit];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(modifyPasswordCell);
        make.left.mas_equalTo(modifyPasswordCell.imageView.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
    }];
    _nPasswordTF = textField;
    
    //底部线
    UIView *line = [[UIView alloc] init];
    [modifyPasswordCell addSubview:line];
    line.backgroundColor = [UIColor lightGrayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.height.mas_offset(.5*FTY_HEIGHT_BASE);
    }];
    
    return modifyPasswordCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <FTYTouchTableViewDelegate>

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [_nPasswordTF resignFirstResponder];
}

#pragma mark - Private Invoke Methods

- (void)modiftyBtnAction:(UIButton *)btn{
    
    NSString *nPassword = _nPasswordTF.text;
    
    if ([FTYInfoVertifyTools isnilVertify:nPassword]) {
        [[FTYToastView alloc] addTitle:@"密码不能为空～" addView:self.view];
    } else {
    
        [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php/?act=ybkapi&op=reg&member_mobile=%@&verify=%@&inv_mobile=%@&password=%@", _phoneNumber, _code, _rePhoneNumber, nPassword] parameters:nil block:^(id result, NSError *error) {
            
            if (!error) {
                
                NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                if (0 == code) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                } else {
                    [[FTYToastView alloc] addTitle:info addView:self.view];
                }
            } else {
                
                NSLog(@"====%@", error);
            }
        }];   
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
